package com.le.core.security.handle;

import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;

import com.alibaba.fastjson.JSON;
import com.le.common.basic.domin.AjaxResult;
import com.le.common.constant.HttpStatus;
import com.le.core.utils.ServletUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

/**
 * 认证失败处理类 返回未授权
 *
 * @author shi
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {

    private static final long serialVersionUID = -8970718410437077606L;

    private static final Logger logger = Logger.getLogger(AuthenticationEntryPointImpl.class.getName());

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        int code = HttpStatus.UNAUTHORIZED;
        String msg = "不经过主人的允许，是请求不到数据的哦~ (●'◡'●)ﾉ♥";
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(code, msg)));
    }
}
